<template>
  <div>
    <!-- pc端显示 -->
    <div class="content_main">
      <div class="aside">
        <div class="aside_wrapper">
          <!-- 搜索功能此本版先不做，暂时注释 -->
          <!-- <div class="aside_search">
            <input
              type="text"
              placeholder="搜索"
              maxlength="100"
              @keyup.enter="handleSearch"
            />
            <img
              src="../assets/images/searchBtn.png"
              alt=""
              @click="handleSearch"
            />
          </div> -->
          <div class="aside_checkbox">
            <div class="group" v-if="osTypeList.length>0">
              <span>OS类型</span>
              <el-checkbox-group
                v-model="osTypeSelectList"
                @change="checkOSType"
              >
                <el-checkbox
                  class="hot"
                  :label="item.osTypeId"
                  v-for="item in osTypeList"
                  :key="item.name"
                  >{{ item.name }}</el-checkbox
                >
              </el-checkbox-group>
            </div>
            <div class="group" v-if="osVersionList.length>0">
              <span>OS版本</span>
              <el-checkbox-group
                v-model="osVersionSelectList"
                @change="checkOSVersion"
              >
                <el-checkbox
                  class="hot"
                  :label="item.osVersionId"
                  v-for="item in osVersionList"
                  :key="item.name"
                  >{{ item.name }}</el-checkbox
                >
              </el-checkbox-group>
            </div>
          </div>
        </div>
      </div>
      <div class="main">
        <router-view></router-view>
      </div>
    </div>
    <!-- 移动端显示 -->
    <div class="mobile_main">
      <div v-if="flag" @click="changeFlag" class="mobile-nav mobile-img">
        <img class="menuImg" src="../assets/images/menuThree.png" />
      </div>
      <div v-else class="mobile-nav mobile-fullScreen">
        <div class="back" @click="goBack">
          <img src="../assets/images/back.png" alt="" />
        </div>
        <div class="aside">
          <div class="aside_wrapper">
            <!-- 搜索功能此本版先不做，暂时注释 -->
            <!-- <div class="aside_search">
            <input
              type="text"
              placeholder="搜索"
              maxlength="100"
              @keyup.enter="handleSearch"
            />
            <img
              src="../assets/images/searchBtn.png"
              alt=""
              @click="handleSearch"
            />
          </div> -->
            <div class="aside_checkbox">
              <div class="group">
                <span>OS类型</span>
                <el-checkbox-group v-model="osTypeSelectList" @change="checkOSTypeMobile">
                  <el-checkbox
                    :label="item.osTypeId"
                    v-for="item in osTypeList"
                    :key="item.name"
                    >{{ item.name }}</el-checkbox
                  >
                </el-checkbox-group>
              </div>
              <div class="group">
                <span>OS版本</span>
                <el-checkbox-group v-model="osVersionSelectList" @change="checkOSVersionMobile">
                  <el-checkbox
                    :label="item.osVersionId"
                    v-for="item in osVersionList"
                    :key="item.name"
                    >{{ item.name }}</el-checkbox
                  >
                </el-checkbox-group>
              </div>
            </div>
            <button class="confirmBtn" @click="handleSearch">确定</button>
          </div>
        </div>
      </div>
      <div v-if="flag" class="main">
        <router-view></router-view>
      </div>
      <div v-else class="main"></div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      curentNoticeId: 1,
      flag: true,
      titleId: 1,
      busTypeId: null,
      osTypeSelectList: ['0'],
      osVersionSelectList: ['0'],
      pageNo: 1,
      pageSize: 9,
    };
  },
  watch: {
    //监听路由变化，选中os类型和版本
    "$route.query"(newVal) {
      // if (newVal) {
      //   this.osTypeSelectList = newVal.osTypeId
      //     ? newVal.osTypeId.split(",")
      //     : ['0'];
      //   this.osVersionSelectList = newVal.osVersionId
      //     ? newVal.osVersionId.split(",")
      //     : ['0'];
      // }
    },
  },
  created() {
    // this.searchAll(1);
    //清空遗留的query
    this.$router.push({ query: {} });
  },
  computed: {
    ...mapState({
      routerList: (state) => state.initData.levelList,
      osTypeList: (state) => state.initData.osTypeList,
      osVersionList: (state) => state.initData.osVersionList,
    }),
  },
  methods: {
    ...mapActions(["getInitData"]),
    //查询接口
    async searchAll() { 
      if (this.$route.query.titleId) {
        this.titleId = this.$route.query.titleId;
      }
      if (this.$route.query.busTypeId) {
        this.busTypeId = this.$route.query.busTypeId;
      }
      let osTypeId =
        this.osTypeSelectList.length > 0 && !(this.osTypeSelectList.length===1 && this.osTypeSelectList[0] === '0')
          ? this.osTypeSelectList.join(",")
          : null;
      let osVersionId =
        this.osVersionSelectList.length > 0 && !(this.osVersionSelectList.length===1 && this.osVersionSelectList[0] === '0')
          ? this.osVersionSelectList.join(",")
          : null;
          
      let query = { titleId: this.$route.query.titleId };
      if (osTypeId) query.osTypeId = osTypeId;
      if (osVersionId) query.osVersionId = osVersionId;
      if (this.busTypeId) query.busTypeId = this.busTypeId

      //更改url
      this.$router.push({
        name: "allFeatures",
        query: query,
      });

      let res = await this.$axios.get(
        "/knowledge/frontPage/queryInitialization",
        {
          params: {
            titleId: this.titleId,
            busTypeId: this.busTypeId,
            osTypeId: osTypeId,
            osVersionId: osVersionId,
            pageNum: this.pageNo,
            pageSize: this.pageSize,
          },
        }
      );
      if (res.data.code == 0) {
        let busTypeList = res.data.data.busTypeList;
        let featuresList = res.data.data.featuresList;
        let sampleList = res.data.data.sampleList;
        let titleList = res.data.data.titleList;
        let osTypeList = res.data.data.osTypeList;
        let osVersionList = res.data.data.osVersionList;
        let totalNum = res.data.totalNum;
        osTypeList.unshift({
          osTypeId: "0",
          name: "全部",
          sort: "1",
        });
        osVersionList.unshift({
          osVersionId: "0",
          name: "全部",
          sort: "1",
        });
        this.getInitData({
          busTypeList,
          featuresList,
          sampleList,
          titleList,
          osTypeList,
          osVersionList,
          totalNum,
        });
      }
    },
    //移动端，点击菜单切换显示样式
    changeFlag() {
      this.flag = false;
      this.$emit("successLoding", this.flag);
    },
    //左侧筛选烂搜索事件
    handleSearch() {},
    /**
     * 选择OS类型
     * 1.选择全部再选择其他选项 === 单个选项
     * 2.选择其他选项再选择全部 === 全部选项
     */
    handleChange(item, list, type){
       if (item.length > 1) {
        let current = item[item.length - 1];
        let index = item.indexOf("0");
        if (index !== -1) {
          if (current === "0") {
            list.splice(0, index);
          } else {
            list.splice(index, 1);
          }
        }
      }
      type === 'pc' && this.handleSearch();
    },
     //PC端选择OS类型
    checkOSType(item) {
      this.handleChange(item, this.osTypeSelectList, 'pc')
    },
    //PC端选择OS版本
    checkOSVersion(item) {
      this.handleChange(item, this.osVersionSelectList, 'pc')
    },
    //移动端选择OS类型
    checkOSTypeMobile(item){
      this.handleChange(item, this.osTypeSelectList, 'mobile')
    },
    //移动端选择OS版本
    checkOSVersionMobile(item){
       this.handleChange(item, this.osVersionSelectList, 'mobile')
    },
    handleSearch() {
      this.flag = true;
      this.searchAll();
      this.$emit("successLoding", this.flag);
    },
    goBack() {
      this.flag = true;
      this.$emit("successLoding", this.flag);
    },
  },
};
</script>
<style lang="less" scoped>
.mobile_main {
  display: none !important;
}
.content_main {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: calc(100vh - 110px);
  overflow: hidden;
  .aside {
    width: 16.6%;
    min-height: calc(100vh - 109px);
    // height: 100%;
    background: #fafafa;
    .aside_wrapper {
      margin: 34px 40px;
      // .aside_search {
      //   position: relative;
      //   // border-bottom: 1px solid #e0e0e0;
      //   input {
      //     width: 100%;
      //     height: 48px;
      //     background: #ffffff;
      //     border-radius: 24px;
      //     border: 1px solid #e0e0e0;

      //     padding: 13px 29px;
      //     box-sizing: border-box;
      //   }
      //   img {
      //     position: absolute;
      //     right: 30px;
      //     top: 50%;
      //     transform: translateY(-50%);
      //   }
      // }
      .aside_checkbox {
        width: 100%;
        // margin-top: 30px;
        // border-top: 1px solid #e0e0e0;
        .group {
          margin: 34px 30px 0 0;
          font-family: HarmonyOS_Sans_SC_Medium;
          span {
            font-size: 16px;
            font-weight: 500;
            color: #000000;
            line-height: 21px;
            font-family: HarmonyOS_Sans_SC_Medium;
          }
          .el-checkbox-group {
            margin-top: 16px;
            ::v-deep {
              .el-checkbox {
                display: flex;
                align-items: center;
                color: #000000;
                height: 19px;
                line-height: 19px;
                .el-checkbox__input {
                  line-height: unset;
                  margin-top: 1px;
                  .el-checkbox__inner {
                    border-color: #727272;
                    // min-width: 12PX;
                    // min-height: 12PX;
                    border-width: 1px;
                    vertical-align: middle;
                  }
                }
                .el-checkbox__input.is-checked {
                  .el-checkbox__inner {
                    background-color: #000000;
                    border-color: #000000;
                    &:after {
                      border-width: 1px;
                    }
                  }
                }
                .el-checkbox__input {
                  .el-checkbox__inner:hover {
                    border-color: #000000;
                  }
                }

                .el-checkbox__label {
                  max-width: 100%;
                  line-height: 19px;
                  padding-left: 13px;
                  position: relative;
                  display: inline-block;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                  vertical-align: text-top;
                }
                .el-checkbox__input.is-checked + .el-checkbox__label {
                  color: #000000;
                }
              }
            }
          }
        }
      }
    }
  }
  .main {
    width: 83.34%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding-left: 80px;
    overflow-y: auto;
    // border: 1px solid black;
    .main-txt {
      height: 22px;
      font-size: 14px;
      font-family: HarmonyOS_Sans_SC;
      color: #000000;
      line-height: 22px;
      margin-top: 40px;
    }
  }
}

@media screen and (max-width: 540px) {
  .content_main {
    display: none !important;
  }
  .mobile_main {
    margin-top: 19px;
    display: block !important;
    width: 100% !important;
    .mobile-img {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: row;
      padding: 0 12px;
      box-sizing: border-box;
      div {
        margin-left: 10px;
        // width: 28px;
        height: 22px;
        font-size: 14px;
        font-family: HarmonyOS_Sans_SC;
        color: #000000;
        line-height: 22px;
      }
    }
    .mobile-nav {
      .aside {
        width: 100%;
        height: 100%;
        background: #fafafa;
        .aside_wrapper {
          margin: 32px 24px;
          .aside_search {
            position: relative;
            // border-bottom: 1px solid #e0e0e0;
            input {
              width: 100%;
              height: 48px;
              background: #ffffff;
              border-radius: 24px;
              border: 1px solid #e0e0e0;

              padding: 13px 29px;
              box-sizing: border-box;
            }
            img {
              position: absolute;
              right: 30px;
              top: 50%;
              transform: translateY(-50%);
            }
          }
          .aside_checkbox {
            width: 100%;
            // margin-top: 30px;
            // border-top: 1px solid #e0e0e0;
            .group {
              margin: 32px 24px 0 0;
              font-family: HarmonyOS_Sans_SC_Medium;
              span {
                font-size: 15px;
                font-weight: 500;
                color: #000000;
                line-height: 23px;
                 font-family: HarmonyOS_Sans_SC_Medium;
              }
              .el-checkbox-group {
                margin-top: 16px;
                ::v-deep {
                  .el-checkbox {
                    display: block;
                    height: 19px;
                    line-height: 19px;
                    .el-checkbox__input {
                      .el-checkbox__inner {
                        border-color: #727272;
                      }
                    }
                    .el-checkbox__input.is-checked {
                      .el-checkbox__inner {
                        background-color: #000000;
                        border-color: #000000;
                      }
                    }
                    .el-checkbox__input {
                      .el-checkbox__inner:hover {
                        border-color: #000000;
                      }
                    }

                    .el-checkbox__label {
                      color: #000000;
                      max-width: 100%;
                      line-height: 19px;
                      padding-left: 13px;
                      position: relative;
                      display: inline-block;
                      overflow: hidden;
                      text-overflow: ellipsis;
                      white-space: nowrap;
                      vertical-align: text-top;
                    }
                    .el-checkbox__input.is-checked + .el-checkbox__label {
                      color: #000000;
                    }
                  }
                  .is-checked {
                    // background-color: unset !important;
                  }
                }
              }
            }
          }
          .confirmBtn {
            color: #fff;
            font-size: 16px;
            width: 327px;
            height: 48px;
            background: #000000;
            border-radius: 31px;
            border: none;
            position: absolute;
            bottom: 41px;
            left: 6.4%;
          }
        }
      }
    }
    .mobile-fullScreen {
      width: 100vw;
      background: #fafafa;
      position: absolute;
      top: 46px;
      min-height: calc(100vh - 46px);
      .back {
        width: 46px;
        height: 46px;
        position: fixed;
        top: 0px;
        left: 0px;
        img {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          left: 10px;
        }
      }
    }
  }
  .main{
    margin-top: 8px;
  }
}
</style>
